import data from "../../../../core/cache_data";
import { ReplacePageAni } from "../../../../core/game";
import Utils, { Sync } from "../../../../core/utils";
import { BulletGameUtils } from "../game/bullet_game_utils";
import Character from "../game/character";
import CharacterCtrl from "../game/character_ctrl";
import Interface from "../game/interface";
import World from "../../world";
import { GameData } from "../game/game_data";
import { WorldData } from "../../world_data";
import { Library } from "../../../../core/library";

const { ccclass, property } = cc._decorator;


@ccclass
export default class PageGame extends cc.Component {
    @property(Interface)
    interface: Interface = null;

    gameData: GameData = null;
    world: World = null;
    char: Character = null;

    @property(cc.Node)
    btnZoomIn: cc.Node = null;
    @property(cc.Node)
    btnZoomOut: cc.Node = null;
    @property(cc.Label)
    lbZoom: cc.Label = null;
    @property(cc.Label)
    lbWorldName: cc.Label = null;
    protected onEnable(): void {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }
    protected onDisable(): void {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }
    zooming = 0;
    protected onLoad(): void {
        this.btnZoomIn.on(cc.Node.EventType.TOUCH_START, () => {
            this.zooming = 1;
        })
        this.btnZoomIn.on(cc.Node.EventType.TOUCH_END, () => {
            this.zooming = 0;
        })
        this.btnZoomOut.on(cc.Node.EventType.TOUCH_START, () => {
            this.zooming = -1;
        })
        this.btnZoomOut.on(cc.Node.EventType.TOUCH_END, () => {
            this.zooming = 0;
        })

    }
    protected lateUpdate(dt: number): void {
        this.interface.zoom += this.zooming * dt;
        this.lbZoom.string = this.interface.zoom.toFixed(2);
    }
    protected start(): void {
        this.gameData = data.cache.get("cur-game-data", null);
        //加入人物
        this.changeWorld(this.gameData.worldData);
        this.reborn();
    }

    async deleteWorld() {
        if (this.world) {
            this.world.node.destroy();
            this.world = null;
        }
    }
    changeWorld(worldData: WorldData) {
        if (this.world) {
            if (this.char) {
                this.char.exitWorld();
            }
            this.deleteWorld();
        }
        //初始化世界
        this.world = BulletGameUtils.InstantiateWorld();
        this.node.addChild(this.world.node);
        this.world.initWithData(worldData);
        this.interface.world = this.world;

        this.world.on("change-world", this.onWorldChangeWorld, this);

        if (this.char) {
            this.char.enterWorld(this.world);
            this.char.curPos = cc.v2(this.world.data.bornPointX * 100, this.world.data.bornPointY * 100);
        }


        //播放世界名字
        this.lbWorldName.string = this.world.data.name;
        cc.Tween.stopAllByTarget(this.lbWorldName.node);
        cc.tween(this.lbWorldName.node).to(0.25, { opacity: 255 }).delay(1).to(0.25, { opacity: 0 }).start();
    }
    onWorldChangeWorld(worldKey: string) {
        let worldData = Library.GetRecord(WorldData, worldKey);
        this.changeWorld(worldData);
    }
    reborn() {
        if (!cc.isValid(this.char)) {
            this.char = BulletGameUtils.InstantiateBullet("player", Character);
            this.char.enterWorld(this.world);
            this.char.node.setPosition(this.world.data.bornPointX * 100, this.world.data.bornPointY * 100);
        }
        this.interface.init(this.char);
    }

    async onClickExit() {
        let prevPage = data.cache.get("prev-page", "page_map_editor");

        Utils.blocker.show();
        await Utils.game.replacePage({
            name: prevPage,
            ani: ReplacePageAni.Fade
        });
        Utils.blocker.hide();
    }

    onTouchStart(evt: cc.Event.EventTouch) {
    }
    onTouchMove(evt: cc.Event.EventTouch) {
    }
    onTouchEnd(evt: cc.Event.EventTouch) {
    }
};